import 'dart:async';

import 'package:flutter_layout/model/article.dart';
import 'package:flutter_layout/model/banner.dart';
import 'package:flutter_layout/provider/view_state_refresh_list_model.dart';
import 'package:flutter_layout/service/wan_android_repository.dart';
import 'package:flutter_layout/tools/print_utils.dart';
import 'package:flutter_layout/utils/platform_utils.dart';

import 'favourite_model.dart';

class HomeModel extends ViewStateRefreshListModel {
  List<Banner>? _banners;
  List<Article>? _topArticles;

  List<Banner>? get banners => _banners;

  List<Article>? get topArticles => _topArticles;

  @override
  Future<List> loadData({int? pageNum}) async {
    PrintUtils.printByApp("执行loadData $pageNum");

    List<Future> futures = [];
    if (pageNum == ViewStateRefreshListModel.pageNumFirst) {
      futures.add(WanAndroidRepository.fetchBanners());
      futures.add(WanAndroidRepository.fetchTopArticles());
    }
    futures.add(WanAndroidRepository.fetchArticles(pageNum!));

    var result = await Future.wait(futures);
    if (pageNum == ViewStateRefreshListModel.pageNumFirst) {
      _banners = result[0];
      _topArticles = result[1];
      return result[2];
    } else {
      return result[0];
    }
  }

   

  void testFuture() async {
    var result = await Future.wait([
      WanAndroidRepository.fetchBanners(),
      // WanAndroidRepository.fetchBanners(),
    ]);
    PrintUtils.printByApp(result[0]);
    // .then((value) => print(value[0] + value[1]));
  }

  @override
  onCompleted(List? data) {
    PrintUtils.printByApp("执行onCompleted  ${data?.length}");
    GlobalFavouriteStateModel.refresh(data);
  }
}
 